//
//  TranditionalBannerCell.m
//  PersonalHospital
//
//  Created by qianfeng on 15/12/11.
//  Copyright (c) 2015年 QianFeng. All rights reserved.
//

#import "TranditionalBannerCell.h"
#import "TranditionalBannerModel.h"
@interface TranditionalBannerCell ()

@property(nonatomic,strong)UIScrollView * scrollView;

@end

@implementation TranditionalBannerCell
{
    UIPageControl * _pagController;
    NSTimer * _timer;

}

-(UIScrollView *)scrollView{
    if (_scrollView==nil) {
        _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreen_Size.width,KTranditional_Banner_Cell)];
    }
    return _scrollView;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addUI];
        [self addTimer];
    }
    return self;
}


-(void)addTimer{
    _timer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timeGo) userInfo:nil repeats:YES];
    _timer.fireDate=[NSDate distantPast];
}

-(void)timeGo{
    NSInteger index=self.scrollView.contentOffset.x/self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width*(index+1), 0) animated:YES];
    if (index==3) {
        index=0;
    }
    _pagController.currentPage=index;
    _index=_pagController.currentPage;
}


-(void)addUI{
    self.scrollView.pagingEnabled=YES;
    self.scrollView.showsHorizontalScrollIndicator=NO;
    self.scrollView.delegate=self;
    self.scrollView.userInteractionEnabled=YES;
    [self.contentView addSubview:self.scrollView];
    
    _pagController=[[UIPageControl alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width-150, self.scrollView.frame.size.height-20, 150, 20)];
    _pagController.currentPageIndicatorTintColor=[UIColor redColor];
    _pagController.pageIndicatorTintColor=[UIColor grayColor];
    

    [self.contentView addSubview:_pagController];
}

-(void)setBannerArray:(NSArray *)bannerArray{
    _bannerArray=bannerArray;
    _pagController.numberOfPages=bannerArray.count;
    _pagController.currentPage=0;
    self.scrollView.contentSize=CGSizeMake(KScreen_Size.width*(bannerArray.count+2),self.scrollView.frame.size.height);
    
    for (int i=0; i<bannerArray.count+2; i++) {
        UIImageView * imageView=[[UIImageView alloc]init];
        TranditionalBannerModel * model=nil;
        if (i==0) {
            model=[bannerArray lastObject];
        }else if (i==bannerArray.count+1){
            model=[bannerArray firstObject];
        }else{
            model=bannerArray[i-1];
        }
       
        imageView.frame=CGRectMake(self.scrollView.frame.size.width*i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.bigImg]];
        
        imageView.userInteractionEnabled=YES;
        
        UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapp:)];
        
        [imageView addGestureRecognizer:tap];
        
        [self.scrollView addSubview:imageView];
    }
    
    self.scrollView.contentOffset=CGPointMake(self.scrollView.frame.size.width, 0);
}

-(void)tapp:(UIGestureRecognizer *)tap{
    if (_imageTap) {
        self.imageTap(_index);
    }
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index=self.scrollView.contentOffset.x/self.scrollView.frame.size.width;
    if (index==self.bannerArray.count+1) {
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
    }else if(index==0){
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width*self.bannerArray.count, 0) animated:NO];
    }
}


@end
